package com.ruoyi.web.controller.business;


import com.ruoyi.business.domain.CivilServants;
import com.ruoyi.business.mapper.CivilServantsMapper;
import com.ruoyi.business.service.CivilServantsService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/businessExcel")
@Api(tags = {"表格导入控制器"})
public class ExcelImportController extends BaseController {

    @Autowired
    CivilServantsService civilServantsService;

    @Autowired
    CivilServantsMapper civilServantsMapper;

    @PostMapping("/import")
    @ApiOperation("表格导入列表接口")
    @PreAuthorize("@ss.hasPermi('index:import')")
    public AjaxResult addList(@RequestPart("file") MultipartFile file) throws IOException {

        //TODO:1.导入文档形成实例，需查询数据库去重。
        //TODO:2.导入的Excel文档模版格式目前固定，按原先模版需去除抬头不必要的单元格
//        civilServantsService.addExcel(file);
        // 获取当前用户ID
        Long userid = SecurityUtils.getUserId();
        civilServantsService.insert(file,userid);
        return success();
    }

    @PostMapping("/importTemplate")
    @ApiOperation("表格模版导入列表接口")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<CivilServants> util = new ExcelUtil<CivilServants>(CivilServants.class);
        util.importTemplateExcel(response, "机关公务员花名册");
    }

    @PostMapping("/export")
    @ApiOperation("表格导出列表接口")
    @PreAuthorize("@ss.hasPermi('index:export')")
    public void export(HttpServletResponse response) throws IOException {
        List<CivilServants> list = civilServantsService.list();
        ExcelUtil<CivilServants> util = new ExcelUtil<CivilServants>(CivilServants.class);
//        util.exportExcel(list, "机关公务员花名册");
        util.exportExcel(response,list, "机关公务员花名册");
    }


}
